const isDev = import.meta.env.DEV;
import axios from 'axios';
import { AlertOutlined, FundOutlined, CrownOutlined, PieChartOutlined, TeamOutlined, ToolOutlined } from "@vicons/antd";
import { closeDialog, showDialog, showConfirmDialog } from "vant";
export let cfg = reactive({
    upd_diy: (type) => {
        let loading;
        //对obj遍历
        const obj = cfg.mldiycss;
        cfg.style_text = ``;
        if(type == 'init'){
            loading = localStorage.getItem("new_diycss") ?? null;
            //如果有的话json字符串转为对象
            if(loading != null){
                const jsondiy = JSON.parse(loading);
                //覆盖对象
                for (const key in jsondiy) {
                    obj[key] = jsondiy[key];
                }
            }

            if(cfg.mldiycss["--ml-bgurl"] == null){
                if(cfg.mobile == true){
                    cfg.mldiycss["--ml-bgurl"] = 'url(https://patchwiki.biligame.com/images/gt/a/a5/07ngp6xg77daci182hve1lixnd8qqu7.png)';
                }else{
                    cfg.mldiycss["--ml-bgurl"] = 'url(https://patchwiki.biligame.com/images/gt/6/6a/tds5pt423aggr7ctz41a8yzy7aa2ii8.png)';
                }
            }
        }
        
        for (const key in obj) {
          cfg.style_text += `${key}:${obj[key]};`;
        }
        if(type == null){
            //把对象保存为json字符串
            const jsondiy = JSON.stringify(obj);
            localStorage.setItem("new_diycss", jsondiy);
        }
      },
    style_text:'',
    mldiycss:{
      '--ml-bodybg' : 'rgba(42, 45, 62, 0.8)',
      '--ml-bodybg-mb' : 'rgba(42, 45, 62, 0.55)',
      '--ml-family' : 'HarmonyOS_Medium, HarmonyOS_Medium !important',
      '--ml-boxmbl' : 20,
      '--ml-boxmbl-mb' : 5,
      '--ml-nonebg' : 'block',
      '--c4' : 'rgba(255, 255, 255, 0.82)',
      '--bdw' : '1200px',
      '--ml-bjvw' : 2.5,
      '--bg5' : 'rgba(49,101,126 , 0.5)',
    },
    drag: true,
    alert2: showConfirmDialog,
    alert3: showDialog,
    offalert: closeDialog,
    get_bname : (icon) => {
        return cfg.role_icon[icon]?.bname[0] ?? '未命名';
    },
    mget: async (obj) => {
        const { url, query } = obj;
        const data = await axios.get(url, {
            params: query,
        });
        return data.data;
    },
    //属性克制判断
    kzpd : (et,roleicon)=>{
        const roleet = cfg.role_icon[roleicon]?.shu;
        const etlist = {
          光 : '虚',
          暗 : '光',
          虚 : '暗',
          水 : '土',
          火 : '水',
          土 : '火',   
        };
        if(etlist[et] == roleet){
            return true;
        }else{
            return false;
        }
    },
    pdlogin :()=>{
        const d = cfg.user;
        if(d?.uid == null){
            return false;
        }
        if(d.uid?.length >= 5){
            return true;
        }else{
            return false;
        }
    },
    //浅深拷贝OBJ
    copy(obj) {
        try {
            return JSON.parse(JSON.stringify(obj));
        } catch (error) {
            cfg.alert3({
                message: `${error}`,
                closeOnClickOverlay: true,
            });
        }
    },
    updfinish: ({
        file,
        event,
        run,
    }) => {
        try {
            const data = event?.target?.response;
            if(data.code == -1){
                file.status = 'error';
                throw {code: -1, message: data.msg};
            }
            if(run != null){
                run(file,data);
            }
            return file;
        } catch (error) {
            cfg.alert3({
                title: 'cfg.finish ERROR',
                message: `${error.message}`,
                closeOnClickOverlay: true,
            });
            return false;
        }
    },
    mpost: async (obj) => {
        const { url, query, post } = obj;
        const data = await cfg.axios.post(url, post, {
            params: query,
        });
        return data.data;
    },
    tss: [
        {
            label: "火队",
            value: "火",
        },
        {
            label: "水队",
            value: "水",
        },
        {
            label: "土队",
            value: "土",
        },
        {
            label: "光队",
            value: "光",
        },
        {
            label: "暗队",
            value: "暗",
        },
        {
            label: "虚队",
            value: "虚",
        },
        {
            label: "通用队",
            value: "通",
        },
    ],
    ss: [
        {
            label: '取消筛选',
            value: null,
        },
        {
            label: '火属性',
            value: '火',
        },
        {
            label: '水属性',
            value: '水',
        },
        {
            label: '土属性',
            value: '土',
        },
        {
            label: '光属性',
            value: '光',
        },
        {
            label: '暗属性',
            value: '暗',
        },
        ,
        {
            label: '虚属性',
            value: '虚',
        }
    ],
    mbset:[],
    tabhand: (id, data) => {
        data.tab = id;
        cfg.router.push({
            query: {
                ...cfg.route.query,
                tab: data.tab ?? null,
            },
        });
    },
    wpicon: (icon, level) => {
        let url = `${cfg.api}gachaimg/`
        if (level >= 6) {
            url += `${icon}(开花).png`;
        } else {
            url += `${icon}.png`;
        }
        return url;
    },
    loginfm: {
        username: "",
        pw: "",
    }, //登录表单
    season: {
        select: [],
    },
    icp: '粤ICP备18120379号',
    bosslist: [],
    bossnamelist: {},
    rt_def: {
        "Rankings": "team_rank",
        "introduction": "team_data",
        "guide": "team_data",
        "tools": "gt_Mastery",
        "guild": "stat",
        "admin": "cloud_cmd",
        "settings": "dddd",
        "theme": "settings",
    },
    qxpd: (lx, uid) => {
        //判断uid是否相同
        let tf = false;
        if (cfg.user?.group?.admin == true) {
            return true;
        }
        lx.forEach(item => {
            switch (item) {
                case 'user': {
                    if (uid == null) {
                    } else {
                        if (uid == cfg.user?.uid) {
                            tf = true;
                            return;
                        }
                    }
                    break;
                }
                case 'bz': {
                    if (cfg.user?.group?.Introduction == true) {
                        tf = true;
                        return;
                    }
                    break;
                }
            }
        });

        if (tf == true) {
            return true;
        } else {
            return false;
        }

    },

    loginif: false, //登录框弹出
    loginout: () => {
        localStorage.removeItem('token');
        cfg.token = null;
        cfg.user = null;
    },
    token: null, //账号token值
    qxname: { //权限名称
        "admin": "超级管理员",
        "bot": "BOT",
        "gfbot_admin": "Bot面板权限",
        "sv_admin": "SV面板权限",
        "gt_all": "坎公全局管理组",
        "Introduction": "攻略管理组",
        "ordinary_user": "用户权限",
    },
    loadlist: {}, //加载时弹出的列表
    cdn2: "https://gtimg.ww2.ren/",
    cdn3: "https://gtimg.ww2.ren/?file=",
    upurl: "https://gtimg.ww2.ren/upload.php",
    api: isDev ? "https://opengm.ww2.ren/" : "https://open.ww2.ren/" , //截图API接口
    ptr_api: "http://192.168.31.200:5222/", //测试API接口
    url: "https://sv2api.ww2.ren/", //服务端地址
    url2: isDev ? "https://opengm.ww2.ren/" : 'https://open.ww2.ren/' ,
    gid: null, //公会id
    guild: {}, //公会数据
    unix:(timestamp) => {
        //获取xxxx-xx-xx xx:xx格式
        const date = new Date(timestamp * 1000);
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hour = date.getHours();
        let minute = date.getMinutes();
        let second = date.getSeconds();
        //保留00
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        if (hour < 10) {
            hour = "0" + hour;
        }
        if (minute < 10) {
            minute = "0" + minute;
        }
        if (second < 10) {
            second = "0" + second;
        }   
        return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
    app: { //背景图片
        img: "open",
        imgdata: "https://patchwiki.biligame.com/images/gt/8/8b/bgnos1tf320k2c0xfvcx1logemk9rkp.jpg",
    },
    loading: false, //全局加载开关
    function: {},
});


